package com.lanou.beautifulwardrobe.Home.Adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.DetailMobileDesc;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class ProductDetailAdapter extends RecyclerView.Adapter<ProductDetailAdapter.DetailViewHolder> {
    private static ProductDetailAdapter adapter;

    public static ProductDetailAdapter getInstance() {
        if (adapter == null) {
            adapter = new ProductDetailAdapter();
        }
        return adapter;
    }

    private List<DetailMobileDesc> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    public void addData(List<DetailMobileDesc> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public DetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_product_detail_item, null);
        return new DetailViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final DetailViewHolder holder, int position) {
        if (datas != null && datas.size() > 0) {
            DetailMobileDesc desc = datas.get(position);
            volleySingleton.getImageResuest(desc.getContent(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    holder.productDetailIv.setImageBitmap(response);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    class DetailViewHolder extends RecyclerView.ViewHolder {
        private ImageView productDetailIv;

        public DetailViewHolder(View itemView) {
            super(itemView);
            productDetailIv = (ImageView) itemView.findViewById(R.id.productDetailIv);
        }
    }
}
